package com.hz.house.service;

import com.hz.house.bean.entity.CityTradeInfoBean;
import com.hz.house.bean.entity.HouseCertHistory;
import com.hz.house.bean.entity.LocalTradeInfoBean;
import com.hz.house.bean.vo.*;
import com.hz.house.dao.CityHouseTradeHistoryMapper;
import com.hz.house.dao.HouseCertHistoryMapper;
import com.hz.house.dao.LocalHouseTradeHistoryMapper;
import com.hz.house.utils.DaysIntUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class HousePriceTradeInfoService {
    @Autowired
    private HouseCertHistoryMapper historyMapper;
    @Autowired
    private CityHouseTradeHistoryMapper cityMapper;
    @Autowired
    private LocalHouseTradeHistoryMapper localMapper;
    public CityTradeInfoWrapperVO listCityTradeInfo(){
        List<CityTradeInfoBean> beans = cityMapper.selectNestLog();
        List<CityTradeInfoVO> vos = new ArrayList<>();

        CityTradeInfoWrapperVO wrapper = new CityTradeInfoWrapperVO();
        wrapper.setVos(vos);

        beans.forEach(item->{
            CityTradeInfoVO vo = new CityTradeInfoVO();
            BeanUtils.copyProperties(item, vo);
            vos.add(vo);
            wrapper.setLastUpdate(item.getUpdateDate());
        });
        return wrapper;
    }

    public LocalTradeInfoWrapperVO listLocalTradeInfo(){
        List<LocalTradeInfoBean> infoBeans = localMapper.listNestTradeInfo();
        LocalTradeInfoWrapperVO wrapper = new LocalTradeInfoWrapperVO();
        wrapper.getBsOtherArea().setTitle("非住宅面积(㎡)");
        wrapper.getBsHouseArea().setTitle("住宅面积(㎡)");
        wrapper.getBsCount().setTitle("住宅套数(套)");
        wrapper.getBsTotalArea().setTitle("总面积(㎡)");
        wrapper.getOldOtherArea().setTitle("非住宅面积(㎡)");
        wrapper.getOldHouseArea().setTitle("住宅面积(㎡)");
        wrapper.getOldCount().setTitle("住宅套数(套)");
        wrapper.getOldTotalArea().setTitle("总面积(㎡)");
        for (LocalTradeInfoBean item: infoBeans){
            if (item.getAreaId() == 1) {//中心城区
                wrapper.getBsCount().setCenterCity(format(item.getBsHomeCount()));
                wrapper.getBsHouseArea().setCenterCity(format(item.getBsHomeArea()));
                wrapper.getBsOtherArea().setCenterCity(format(item.getBsOtherArea()));
                wrapper.getBsTotalArea().setCenterCity(format(item.getBsTotalArea()));
                wrapper.getOldCount().setCenterCity(format(item.getOldHomeCount()));
                wrapper.getOldHouseArea().setCenterCity(format(item.getOldHomeArea()));
                wrapper.getOldOtherArea().setCenterCity(format(item.getOldOtherArea()));
                wrapper.getOldTotalArea().setCenterCity(format(item.getOldTotalArea()));
            }else if (item.getAreaId() == 2){//郊区新城
                wrapper.getBsCount().setNearCity(format(item.getBsHomeCount()));
                wrapper.getBsHouseArea().setNearCity(format(item.getBsHomeArea()));
                wrapper.getBsOtherArea().setNearCity(format(item.getBsOtherArea()));
                wrapper.getBsTotalArea().setNearCity(format(item.getBsTotalArea()));
                wrapper.getOldCount().setNearCity(format(item.getOldHomeCount()));
                wrapper.getOldHouseArea().setNearCity(format(item.getOldHomeArea()));
                wrapper.getOldOtherArea().setNearCity(format(item.getOldOtherArea()));
                wrapper.getOldTotalArea().setNearCity(format(item.getOldTotalArea()));
            }else if (item.getAreaId() == 3){//全市
                wrapper.getBsCount().setAllCity(format(item.getBsHomeCount()));
                wrapper.getBsHouseArea().setAllCity(format(item.getBsHomeArea()));
                wrapper.getBsOtherArea().setAllCity(format(item.getBsOtherArea()));
                wrapper.getBsTotalArea().setAllCity(format(item.getBsTotalArea()));
                wrapper.getOldCount().setAllCity(format(item.getOldHomeCount()));
                wrapper.getOldHouseArea().setAllCity(format(item.getOldHomeArea()));
                wrapper.getOldOtherArea().setAllCity(format(item.getOldOtherArea()));
                wrapper.getOldTotalArea().setAllCity(format(item.getOldTotalArea()));
            }
            wrapper.setLastUpdate(item.getUpdateDate());
        }
        return wrapper;
    }

    public String format(Object o){
        return Objects.isNull(o) ? "":String.valueOf(o);
    }

    public HouseCertHistoryWrapperVO listWeekOpenInfo(Date date){
        Date _date;
        if (Objects.isNull(date)){
            _date = DaysIntUtil.dateNextDay(new Date(), -7);
        }else{
            _date = date;
        }

        Date weekStart = DaysIntUtil.getWeekStart(_date);
        //按周查询出周楼盘开盘统计信息
        List<HouseCertHistory> list = historyMapper.listCertHistory(weekStart);
        //如果是查询上周统计信息,并且没有统计信息则需要统计并写入数据库
        if (Objects.isNull(date) && list.isEmpty()){
            Date weekEnd = DaysIntUtil.getWeekEnd(_date);
            list = historyMapper.selectForDate(weekStart, weekEnd);
            for (HouseCertHistory bean: list){
                bean.setWeekDayStart(weekStart);
                bean.setFixInfo(getDeco(bean.getFixInfo()));
                historyMapper.inser(bean);
            }
        }
        int houseCount = 0;
        //封装统计信息到前端
        List<HouseCertHistoryVO> vos = new ArrayList<>();
        for (HouseCertHistory bean : list){
            HouseCertHistoryVO vo = new HouseCertHistoryVO();
            BeanUtils.copyProperties(bean, vo);
            vos.add(vo);
            houseCount += Objects.isNull(bean.getHouseCount()) ? 0 : bean.getHouseCount();
        }
        HouseCertHistoryWrapperVO wrapper = new HouseCertHistoryWrapperVO();
        wrapper.setBuildingCount(list.size());
        wrapper.setHouseCount(houseCount);
        wrapper.setList(vos);
        //封装最近四周统计信息
        List<Date> dates = new ArrayList<>();
        weekStart = DaysIntUtil.getWeekStart(new Date());
        dates.add(DaysIntUtil.dateNextDay(weekStart, -7));
        dates.add(DaysIntUtil.dateNextDay(weekStart, -14));
        dates.add(DaysIntUtil.dateNextDay(weekStart, -21));
        dates.add(DaysIntUtil.dateNextDay(weekStart, -28));
        wrapper.setHistories(dates);
        return wrapper;
    }
    private String getDeco(String deco) {
        if (deco == null)
            return "";
        if (deco.equals("rinsing"))
            return "清水";
        if (deco.equals("hardcover"))
            return "精装";
        return "精装/清水";
    }
}
